﻿
namespace NetROS.Tiers.Windows.Forms
{
	/// <summary>
	/// abstract DokumentDodatak typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class DokumentDodatakDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<DokumentDodatakDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.DokumentDodatak _currentDokumentDodatak = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxDokumentDodatakDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxDokumentDodatakErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxDokumentDodatakBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the ID property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIDDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the IDDokument property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxIDDokumentDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Opis property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxOpisDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Iznos property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIznosDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.Dokument> _IDDokumentList;
		
		/// <summary> 
		/// The list of selectable Dokument
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.Dokument> IDDokumentList
		{
			get {return this._IDDokumentList;}
			set 
			{
				this._IDDokumentList = value;
				this.uxIDDokumentDataGridViewColumn.DataSource = null;
				this.uxIDDokumentDataGridViewColumn.DataSource = this._IDDokumentList;
			}
		}
		
		private bool _allowNewItemInIDDokumentList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of Dokument
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInIDDokumentList
		{
			get { return _allowNewItemInIDDokumentList;}
			set
			{
				this._allowNewItemInIDDokumentList = value;
				this.uxIDDokumentDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.DokumentDodatak> _DokumentDodatakList;
				
		/// <summary> 
		/// The list of DokumentDodatak to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.DokumentDodatak> DokumentDodatakList
		{
			get {return this._DokumentDodatakList;}
			set
			{
				this._DokumentDodatakList = value;
				this.uxDokumentDodatakBindingSource.DataSource = null;
				this.uxDokumentDodatakBindingSource.DataSource = value;
				this.uxDokumentDodatakDataGridView.DataSource = null;
				this.uxDokumentDodatakDataGridView.DataSource = this.uxDokumentDodatakBindingSource;				
				//this.uxDokumentDodatakBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxDokumentDodatakBindingSource_ListChanged);
				this.uxDokumentDodatakBindingSource.CurrentItemChanged += new System.EventHandler(OnDokumentDodatakBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnDokumentDodatakBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentDokumentDodatak = uxDokumentDodatakBindingSource.Current as Entities.DokumentDodatak;
			
			if (_currentDokumentDodatak != null)
			{
				_currentDokumentDodatak.Validate();
			}
			//_DokumentDodatak.Validate();
			OnCurrentEntityChanged();
		}

		//void uxDokumentDodatakBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.DokumentDodatak"/> instance.
		/// </summary>
		public Entities.DokumentDodatak SelectedDokumentDodatak
		{
			get {return this._currentDokumentDodatak;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxDokumentDodatakDataGridView.VirtualMode;}
			set
			{
				this.uxDokumentDodatakDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxDokumentDodatakDataGridView.AllowUserToAddRows;}
			set {this.uxDokumentDodatakDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxDokumentDodatakDataGridView.AllowUserToDeleteRows;}
			set {this.uxDokumentDodatakDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxDokumentDodatakDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxDokumentDodatakDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="DokumentDodatakDataGridViewBase"/> class.
		/// </summary>
		public DokumentDodatakDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxDokumentDodatakDataGridView = new System.Windows.Forms.DataGridView();
			this.uxDokumentDodatakBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxDokumentDodatakErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIDDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIDDokumentDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxOpisDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIznosDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxIDDokumentBindingSource = new DokumentBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDDokumentBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDokumentDodatakDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDokumentDodatakBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDokumentDodatakErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxDokumentDodatakErrorProvider
			// 
			this.uxDokumentDodatakErrorProvider.ContainerControl = this;
			this.uxDokumentDodatakErrorProvider.DataSource = this.uxDokumentDodatakBindingSource;						
			// 
			// uxDokumentDodatakDataGridView
			// 
			this.uxDokumentDodatakDataGridView.AutoGenerateColumns = false;
			this.uxDokumentDodatakDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxDokumentDodatakDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIDDataGridViewColumn,
		this.uxIDDokumentDataGridViewColumn,
		this.uxOpisDataGridViewColumn,
		this.uxIznosDataGridViewColumn			});
			this.uxDokumentDodatakDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxDokumentDodatakDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxDokumentDodatakDataGridView.Name = "uxDokumentDodatakDataGridView";
			this.uxDokumentDodatakDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxDokumentDodatakDataGridView.TabIndex = 0;	
			this.uxDokumentDodatakDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxDokumentDodatakDataGridView.EnableHeadersVisualStyles = false;
			this.uxDokumentDodatakDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnDokumentDodatakDataGridViewDataError);
			this.uxDokumentDodatakDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnDokumentDodatakDataGridViewCellValueNeeded);
			this.uxDokumentDodatakDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnDokumentDodatakDataGridViewCellValuePushed);
			
			//
			// uxIDDataGridViewColumn
			//
			this.uxIDDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDataGridViewColumn.DataPropertyName = "ID";
			this.uxIDDataGridViewColumn.HeaderText = "ID";
			this.uxIDDataGridViewColumn.Name = "uxIDDataGridViewColumn";
			this.uxIDDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDataGridViewColumn.ReadOnly = true;		
			//
			// uxIDDokumentDataGridViewColumn
			//
			this.uxIDDokumentDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIDDokumentDataGridViewColumn.DataPropertyName = "IDDokument";
			this.uxIDDokumentDataGridViewColumn.HeaderText = "IDDokument";
			this.uxIDDokumentDataGridViewColumn.Name = "uxIDDokumentDataGridViewColumn";
			this.uxIDDokumentDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIDDokumentDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIDDokumentDataGridViewColumn.ReadOnly = false;		
			//
			// uxOpisDataGridViewColumn
			//
			this.uxOpisDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxOpisDataGridViewColumn.DataPropertyName = "Opis";
			this.uxOpisDataGridViewColumn.HeaderText = "Opis";
			this.uxOpisDataGridViewColumn.Name = "uxOpisDataGridViewColumn";
			this.uxOpisDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxOpisDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxOpisDataGridViewColumn.ReadOnly = false;		
			//
			// uxIznosDataGridViewColumn
			//
			this.uxIznosDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIznosDataGridViewColumn.DataPropertyName = "Iznos";
			this.uxIznosDataGridViewColumn.HeaderText = "Iznos";
			this.uxIznosDataGridViewColumn.Name = "uxIznosDataGridViewColumn";
			this.uxIznosDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIznosDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIznosDataGridViewColumn.ReadOnly = false;		
			//
			// uxIDDokumentDataGridViewColumn
			//				
			this.uxIDDokumentDataGridViewColumn.DisplayMember = "IDDokumentTip";	
			this.uxIDDokumentDataGridViewColumn.ValueMember = "ID";	
			this.uxIDDokumentDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxIDDokumentDataGridViewColumn.DataSource = uxIDDokumentBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxDokumentDodatakDataGridView);
			this.Name = "DokumentDodatakDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxIDDokumentBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDokumentDodatakErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDokumentDodatakDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDokumentDodatakBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				DokumentDodatakDataGridViewEventArgs args = new DokumentDodatakDataGridViewEventArgs();
				args.DokumentDodatak = _currentDokumentDodatak;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class DokumentDodatakDataGridViewEventArgs : System.EventArgs
		{
			private Entities.DokumentDodatak	_DokumentDodatak;
	
			/// <summary>
			/// the  Entities.DokumentDodatak instance.
			/// </summary>
			public Entities.DokumentDodatak DokumentDodatak
			{
				get { return _DokumentDodatak; }
				set { _DokumentDodatak = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxDokumentDodatakDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnDokumentDodatakDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxDokumentDodatakDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnDokumentDodatakDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxDokumentDodatakDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						e.Value = DokumentDodatakList[e.RowIndex].ID;
						break;
					case "uxIDDokumentDataGridViewColumn":
						e.Value = DokumentDodatakList[e.RowIndex].IDDokument;
						break;
					case "uxOpisDataGridViewColumn":
						e.Value = DokumentDodatakList[e.RowIndex].Opis;
						break;
					case "uxIznosDataGridViewColumn":
						e.Value = DokumentDodatakList[e.RowIndex].Iznos;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxDokumentDodatakDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnDokumentDodatakDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxDokumentDodatakDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIDDataGridViewColumn":
						DokumentDodatakList[e.RowIndex].ID = (System.Int32)e.Value;
						break;
					case "uxIDDokumentDataGridViewColumn":
						DokumentDodatakList[e.RowIndex].IDDokument = (System.Int32)e.Value;
						break;
					case "uxOpisDataGridViewColumn":
						DokumentDodatakList[e.RowIndex].Opis = (System.String)e.Value;
						break;
					case "uxIznosDataGridViewColumn":
						DokumentDodatakList[e.RowIndex].Iznos = (System.Decimal)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
